local CommonOHOH = _G.CommonOHOH;
local M = CommonOHOH.class.new(script,script.Parent.Base);
local ConnectClass=_G.CommonOHOH.load.Connect();

-- 钻头
function M.new(...)
	return M(...);
end;

function M.ctor(self,...)--构造函数
	M.super.ctor(self,...);
	
	--body更新，点位发生变化，自己也得更新
	self.Car.BodyUpdateEvent:Connect(function()
			if self.CarId then
				self:Update(self.CarId)
			end
	end)


end;

function M.dtor(self)
	M.super.dtor(self);
end;
--组装到整车上（子类实现）
function M.Assemble(self)
	self.Body=self.Car:GetBody()

	-- self.Instance = {}
	-- local drill=self.Model.Instance
	self.Instance.Name= "Weapon"
	-- table.insert(self.Instance, drill)

	self.Instance:PivotTo(self.Body:GetWeaponPivot())
	self.Instance.Parent= self.Body:GetInstance()
	self:Weld2Part(self.Instance,self.Body:GetInstance())

	local value =  self.Instance:WaitForChild("Value").Value
	local weaponsFolder = self.Instance:FindFirstChild("weapons")

	local folder = Instance.new("Folder",self.Body:GetInstance())
	folder.Name = "WeaponMesh"

--碰撞体
	local newPart = Instance.new("Part")
    newPart.Size =self.BoundingSize -- 设置Part大小
    newPart.Color = Color3.fromRGB(255, 0, 0)  -- 设置颜色
	newPart.Transparency = 1
	newPart.Name = "CarCollider"
	newPart.CanTouch = true  -- 设置Part可触摸
    newPart.CanCollide = true  -- 禁止碰撞
    newPart.Massless = true  -- 设置Massless为false
    newPart.Anchored = false  -- 确保Part不被固定
	
	-- 创建一个新的物理属性
	local customProps = PhysicalProperties.new(
		0.7,  -- 密度 Density
		0.8,   -- 摩擦力
		1,     -- 摩擦权重
		0.01,   -- 弹性
		1      -- 弹性权重
	)

-- 应用自定义物理属性到部件
	newPart.CustomPhysicalProperties = customProps
	newPart:PivotTo(self.Body:GetWeaponPivot())

	newPart.CFrame = CFrame.new(
		newPart.CFrame.Position.X,
		newPart.CFrame.Position.Y,
		newPart.CFrame.Position.Z - newPart.Size.Z * 0.5
	)
	newPart.Parent = folder
	self.CollidePart = newPart
	self:Weld2Part(newPart,self.Instance)

--根据不同weapon组装
	if value == 1 then
		self.WeaponType = 1 
		local weapons = weaponsFolder:GetChildren()
		local isFirst =true
		local alignMainAtt = nil
		for _, child in pairs(weapons) do
			if child.ClassName == "MeshPart"  then
				--		WeldCons(child,s)
				child.Massless = true
				local attachment0 = Instance.new("Attachment")
				attachment0.Name = "drillWeapon"..child.Name
				attachment0.Parent = self.Instance
				attachment0.WorldCFrame = child.CFrame
				attachment0.WorldOrientation = Vector3.new(0, 90, 0)

				local attachment1 = Instance.new("Attachment")
				attachment1.Name = "WeaponAtt"..child.Name
				attachment1.Parent = child
				attachment1.WorldCFrame = child.CFrame
				attachment1.WorldOrientation = Vector3.new(0, 90, 0)

				local hinge = Instance.new("HingeConstraint")
				if isFirst==true then--给第一个被设置的武器设置为主武器。副武器将同步主武器的旋转
					hinge.ActuatorType = Enum.ActuatorType.Motor
					hinge.Name = "WeaponController"
					alignMainAtt=attachment1
					isFirst =false
					self.Hinge = hinge
				else
					--local attAO = Instance.new("Attachment")
					--attAO.Name = "attAlignOr"
					--attAO.Parent = child
					if alignMainAtt then
						local alignO  = Instance.new("AlignOrientation")
						alignO.Attachment0 = attachment1
						alignO.Attachment1 = alignMainAtt
						alignO.Parent = child
						hinge.Name = "WeaponAlignHinge"
					end
					
				end
				-- 连接两个 Attachment
				hinge.Attachment0 = attachment0
				hinge.Attachment1 = attachment1

				-- 设置 HingeConstraint 的父级（通常设为第一个物体）
				hinge.Parent = self.Body:GetInstance()
				
				
			end
		end
		
	elseif value  == 2 then
		self.WeaponType = 2	
		local inst2 = self.Instance:FindFirstChild("Instance2")
		self:Weld2Part(inst2,self.Instance)
		
		local weapons = weaponsFolder:GetChildren()
		local isFirst =true
		local alignMainAtt = nil
		
		for _, child in pairs(weapons) do
			if child.ClassName == "MeshPart"  then
				--		WeldCons(child,s)

				local attachment0 = Instance.new("Attachment")
				attachment0.Name = "drillWeapon"..child.Name
				attachment0.Parent = self.Instance
				attachment0.WorldCFrame = child.CFrame
				attachment0.WorldOrientation = Vector3.new(0, 0, 0)

				local attachment1 = Instance.new("Attachment")
				attachment1.Name = "WeaponAtt"..child.Name
				attachment1.Parent = child
				attachment1.WorldCFrame = child.CFrame
				attachment1.WorldOrientation = Vector3.new(0, 0, 0)

				local hinge = Instance.new("HingeConstraint")
				if isFirst==true then
					hinge.ActuatorType = Enum.ActuatorType.Motor
					hinge.Name = "WeaponController"
					alignMainAtt=attachment1
					isFirst =false
					self.Hinge = hinge
				else
					--local attAO = Instance.new("Attachment")
					--attAO.Name = "attAlignOr"
					--attAO.Parent = child
					if alignMainAtt then
						local alignO  = Instance.new("AlignOrientation")
						alignO.Attachment0 = attachment1
						alignO.Attachment1 = alignMainAtt
						alignO.Parent = child
						hinge.Name = "WeaponAlignHinge"
					end

				end
				-- 连接两个 Attachment
				hinge.Attachment0 = attachment0
				hinge.Attachment1 = attachment1

				-- 设置 HingeConstraint 的父级（通常设为第一个物体）
				hinge.Parent = self.Body:GetInstance()


			end
		end
	end
    -- for _, protect in pairs( self.Instance:GetChildren()) do
    -- 	protect.Parent = folder
	-- 	self:Weld2Part(protect,self.Body:GetInstance())
	-- 	-- table.insert(self.Instance, protect)
    -- end

	-- local att0 = Instance.new("Attachment",self.Body:GetInstance())
	-- att0.Name = "weapon1"
	-- att0.WorldPosition = self.Body:GetWeaponPostion()
	-- att0.WorldOrientation =self.Body:GetWeaponOrientation() + Vector3.new(0,90,0)
	
	-- local att1 = Instance.new("Attachment",self.Instance)
	-- att1.Name = "weapon2 "
	-- att1.WorldPosition =  self.Body:GetWeaponPostion()
	-- att1.WorldOrientation =self.Body:GetWeaponOrientation() + Vector3.new(0,90,0)
	
	-- local hin = Instance.new("HingeConstraint",self.Body:GetInstance())
	-- hin.Name = "weapon"
	-- hin.Attachment0 = att0
	-- hin.Attachment1 = att1
	-- hin.ActuatorType = Enum.ActuatorType.Motor
	-- self.Hinge = hin
end

--设置转速
function M.SetDrillingSpeed(self,speed)
	if self.WeaponType == 1 then
		self.Hinge.AngularVelocity = speed  
	else
		self.Hinge.AngularVelocity = -speed  
		--齿轮轮胎需要逆时针旋转
	end
	
	self.Hinge.MotorMaxTorque = 3000
end

-- 获取组装到车上的实例（子类实现）
function M.GetInstance(self)
	return self.Instance
end

--获取钻头的碰撞题
function M.GetTouchPart(self)
	return self.CollidePart
end



return M